package com.nidu.demo.dept.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.dept.convertor.DepartmentCriteriaConvertor;
import com.nidu.demo.dept.dto.DepartmentPageQry;
import com.nidu.demo.dept.convertor.DepartmentConvertor;
import com.nidu.demo.dept.dto.DepartmentTreeCO;
import com.nidu.demo.dept.gateway.DepartmentGateway;
import com.nidu.demo.dept.model.Department;
import com.nidu.demo.dept.model.DepartmentCriteria;
import com.nidu.demo.dept.util.DepartmentUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
/**
 * 部门树形查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class DepartmentPageQryExe {

    private final DepartmentGateway gateway;

    private final DepartmentConvertor convertor;

    private final DepartmentCriteriaConvertor criteriaConvertor;


    public PageResponse<DepartmentTreeCO> execute(DepartmentPageQry qry) {
        // 1. 查询部门列表
        DepartmentCriteria criteria = criteriaConvertor.toDataObject(qry);
        PageResponse<Department> page = gateway.pageByCondition(criteria);
        // 2. 转换为树形结构对象
        List<DepartmentTreeCO> deptTreeCOs = convertor.toTreeClientObjects(page.getData());
        return PageResponse.of(DepartmentUtil.buildDeptTree(deptTreeCOs), page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }
} 